package com.lonely.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lonely.common.enums.CommonStatus;
import com.lonely.common.utils.SecurityUtils;
import com.lonely.goods.domain.dto.goods.GoodsQuotationSheetPartDTO;
import com.lonely.goods.domain.entity.SysGoodsPart;
import com.lonely.goods.service.IGoodsPartService;
import com.lonely.order.domain.entity.SysOrder;
import com.lonely.order.domain.entity.SysOrderPart;
import com.lonely.order.mapper.SysOrderPartMapper;
import com.lonely.order.service.IOrderPartMaterialService;
import com.lonely.order.service.IOrderPartService;
import com.lonely.order.service.IOrderProcessService;
import com.lonely.order.service.IOrderSecondProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 黄志标
 * @date: 2025/6/25 15:42
 * @description: 订单部件接口实现
 */
@Service
public class OrderPartServiceImpl extends ServiceImpl<SysOrderPartMapper, SysOrderPart> implements IOrderPartService {

    @Autowired
    private IOrderPartMaterialService orderPartMaterialService;

    @Autowired
    private IOrderSecondProcessService orderSecondProcessService;

    @Autowired
    private IOrderProcessService orderProcessService;

    @Autowired
    private IGoodsPartService goodsPartService;


    /**
     * 创建订单部件相关数据
     *
     * @param sysOrder
     * @param goodsId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createOrderParts(SysOrder sysOrder, Long goodsId) {
        List<SysGoodsPart> sysGoodsParts = this.goodsPartService.list(new LambdaQueryWrapper<SysGoodsPart>()
                .eq(SysGoodsPart::getGoodsId, goodsId)
                .eq(SysGoodsPart::getStatus, CommonStatus.ENABLE.code)
        );
        if (CollUtil.isEmpty(sysGoodsParts)) {
            return;
        }

        for (SysGoodsPart sysGoodsPart : sysGoodsParts) {
            // 创建订单部件
            SysOrderPart sysOrderPart = this.doingCreateOrderPart(sysGoodsPart, sysOrder);

            // 创建订单物料
            this.orderPartMaterialService.createOrderPartMaterials(sysOrderPart, sysGoodsPart.getId());

            // 创建订单二次工艺
            this.orderSecondProcessService.createOrderSecondProcess(sysOrderPart, sysGoodsPart.getId(),sysOrder);

            // 创建订单工序
            this.orderProcessService.createOrderProcess(sysOrderPart, sysGoodsPart.getId());
        }
    }

    /**
     * 查询指定订单的报价单部件物料信息
     *
     * @param orderId
     * @return
     */
    @Override
    public List<GoodsQuotationSheetPartDTO> queryQuotationSheetPartDetail(Long orderId) {
        Assert.notNull(orderId, "订单id不能为空");
        List<SysOrderPart> orderParts = super.list(new LambdaQueryWrapper<SysOrderPart>()
                .eq(SysOrderPart::getOrderId, orderId)
                .eq(SysOrderPart::getStatus, CommonStatus.ENABLE.code)
        );
        if (CollUtil.isEmpty(orderParts)) {
            return new ArrayList<>();
        }

        return orderParts.stream().map(orderPart -> {
            GoodsQuotationSheetPartDTO quotationSheetPartDTO = new GoodsQuotationSheetPartDTO();
            quotationSheetPartDTO.setPartId(orderPart.getId());
            quotationSheetPartDTO.setPartLibrary(orderPart.getPartLibrary());

            // 获取部件对应的物料信息
            quotationSheetPartDTO.setPartMaterials(this.orderPartMaterialService.queryQuotationSheetPartMaterials(orderPart.getId()));

            // 获取部件对应的二次工艺信息
            quotationSheetPartDTO.setPartSecondProcesses(this.orderSecondProcessService.queryQuotationSheetPartSecondProcesses(orderPart.getId()));

            // 获取部件对应的成衣二次工艺数据
            quotationSheetPartDTO.setPartReadyToWearSecondProcesses(this.orderSecondProcessService.queryQuotationSheetPartReadyToWearSecondProcesses(orderPart.getId()));

            // 获取部件对应的工序数据
            quotationSheetPartDTO.setPartProcess(this.orderProcessService.queryQuotationSheetPartProcesses(orderPart.getId()));
            return quotationSheetPartDTO;
        }).collect(Collectors.toList());
    }

    /**
     * 执行创建订单部件
     *
     * @param sysGoodsPart
     * @param order
     * @return
     */
    private SysOrderPart doingCreateOrderPart(SysGoodsPart sysGoodsPart, SysOrder order) {
        SysOrderPart sysOrderPart = new SysOrderPart();
        sysOrderPart.setOrderId(order.getId());
        sysOrderPart.setProduceOrderId(Convert.toStr(order.getProduceOrderId()));
        sysOrderPart.setPartLibrary(sysGoodsPart.getPartLibrary());
        sysOrderPart.setQuantity(sysGoodsPart.getQuantity());
        sysOrderPart.setType(sysGoodsPart.getType());
        sysOrderPart.setSourceGoodsPartId(sysGoodsPart.getId());
        sysOrderPart.setCreateBy(SecurityUtils.getUserId());
        sysOrderPart.setUpdateBy(SecurityUtils.getUserId());
        super.save(sysOrderPart);
        return sysOrderPart;
    }

}
